/*
 * Created on Sep 24, 2007
 */
package com.mgmaps.utils;

import javax.microedition.io.*;
import javax.wireless.messaging.*;

/**
 * This class that deals with sending SMS messages. It can send place locations 
 * to the inbox or to another MGMaps listening on port 4627.
 *
 * @author CristiS
 * @version 1.0
 */
public class SMSSender implements ISMSSender {
  /**
   * Default constructor.
   */
  public SMSSender() {
  }
  
  /**
   * Send message
   *
   * @param phoneNo phone number to send message to
   * @param msgData message data
   */
  public void sendMessage(String phoneNo, String msgData) throws Exception {
    String addr = "sms://" + phoneNo;
    ExtLogger.debug("Sending SMS to " + addr);
    MessageConnection mc = null;
    try {
      mc = (MessageConnection) Connector.open(addr);
      TextMessage msg = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE);
      
      msg.setPayloadText(msgData);
      mc.send(msg);
    }
    finally {
      if (mc != null)
        try {
          mc.close();
        }
        catch (Exception ex) {
        }
    }    
  }
}
